package com.app.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Queue;

import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.Html;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import app.com.ModuleRole.R;

import com.app.Controller.ActivityController;
import com.app.Controller.NetworkController;
import com.app.Controller.ReadWriteFileController;
import com.app.DTO.AccountDTO;
import com.app.DTO.RoleDTO;
import com.app.Network.NetworkLayer;
import com.app.PhanQuyen.Quyen;

public class frmPhuActivity extends Activity {
	public static Handler handler = new Handler();
	public static Runnable raShowMessage;
	public static Runnable raMoiVaoNhomNotif;
	public static Runnable raUpdateNguoiTrongNhom;
	public static Runnable raKhongDuocVaoNhom;
	public static Runnable raDaVaoNhom;
	public static Context baseContext;
	public static String contentTitle, contentText;
	AccountDTO account = null;
	ArrayList<Quyen> rule = null;
	static String loi = "";
	int port = 5033;
	Button btnManHinhCay, btnXemVaiTro, btnTaoNhom, btnPhu, btnTruc, btnAn,btnThanh;
	TextView txtRole, txtDangNhap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_phu_main);
		ActivityController.setPeerActivity(this);

		btnThanh = (Button) findViewById(R.id.btnThanh);
		btnManHinhCay = (Button) findViewById(R.id.btnManHinhCay);
		btnTaoNhom = (Button) findViewById(R.id.btnTaoNhom1);
		txtRole = (TextView) findViewById(R.id.txtRole);
		btnPhu = (Button) findViewById(R.id.btnPhu);
		btnTruc = (Button) findViewById(R.id.btnTruc);
		btnAn = (Button) findViewById(R.id.btnAn);
		txtDangNhap = (TextView) findViewById(R.id.txtDangNhap);

		
		btnManHinhCay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//Intent intent = new Intent(getBaseContext(), frmTreeNodeActivity.class);
				
				Intent intent = new Intent(getBaseContext(), tabActivity.class);
				startActivity(intent);
				
			}
		});
		btnTaoNhom.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(), frmTaoNhom.class);
				startActivity(intent);

			}
		});
		btnPhu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				NetworkController._peer.account.set_Username("phu");
				NetworkController._peer.account.isThanhLapNhom = false;
				txtDangNhap.setText(NetworkController._peer.account.get_Username());
			}
		});
		btnTruc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				NetworkController._peer.account.set_Username("truc");
				txtDangNhap.setText(NetworkController._peer.account.get_Username());
			}
		});
		btnAn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				NetworkController._peer.account.set_Username("an");
				txtDangNhap.setText(NetworkController._peer.account.get_Username());
			}
		});
		btnThanh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				NetworkController._peer.account.set_Username("thanh");
				txtDangNhap.setText(NetworkController._peer.account.get_Username());
			}
		});
		raShowMessage = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				showMessage(loi);
			}
		};
		raMoiVaoNhomNotif = new Runnable() {

			@Override
			public void run() {
				if (kiemTraVaoNhom()) {
					displayNotification("Có người mời bạn vào nhóm!", contentText, contentTitle);
				}
			}
		};
		raUpdateNguoiTrongNhom = new Runnable() {

			@Override
			public void run() {
				capNhatNguoiTrongNhom(1);

			}
		};
		raKhongDuocVaoNhom = new Runnable() {

			@Override
			public void run() {
				show("Nhóm đã full, ko vào đc :))");

			}
		};
		raDaVaoNhom = new Runnable() {

			@Override
			public void run() {
				show("Da vao nhom roi do");

			}
		};
			ReadWriteFileController.deleteFile(ReadWriteFileController.TREE_PATH);
	}

	public void showMessage(String message) {

		AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setMessage(message);
		alert.setTitle("test");
		alert.show();
	}

	public static void show(String message) {
		loi = message;
		handler.post(raShowMessage);

	}

	@Override
	public void onStart() {
		super.onStart();
		if (NetworkController._peer == null) {
			String s = "";
			AccountDTO account = new AccountDTO();
			account.isThanhLapNhom=false;
			account.set_Username("quocphu");
			ArrayList<RoleDTO> roleList = ReadWriteFileController.readRole(null,ReadWriteFileController.MY_ROLE_PATH);

			for (RoleDTO role : roleList) {
				account.addRole(role);
				Log.i("Phu","Role: "+role.getName());
			}
			NetworkController.init("MES", port, account);		
			//for(Quyen)
		}
		
		
		this.setTitle(NetworkController._peer.getAddressPeer());

	}

	public void displayNotification(String tickerText, String contentTitle, String contentText) {

		Log.i("Phu","trong displayNotification:   "+tickerText+" : "+contentText+" : "+contentTitle);
		
		Intent intent = new Intent(this, frmNotificationMoiVaoNhom.class);
		intent.putExtra("ID", 1);
		intent.putExtra("tickerText", tickerText);
		intent.putExtra("contentTitle", contentTitle);
		intent.putExtra("contentText", contentText);
		
	
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notif = new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis());
		String[] m = contentText.split("#~");
		notif.setLatestEventInfo(this, m[0] + " mời bạn vào nhóm!", contentTitle, pendingIntent);
		notif.flags|=Notification.FLAG_AUTO_CANCEL;
		notif.when=1;
		notiManager.notify(1, notif);
		
	}

	private void capNhatNguoiTrongNhom(int soNguoi) {
		TextView txtSoNguoi = (TextView) findViewById(R.id.txtSoNguoiTrongNhom);
		txtSoNguoi.setText("Số người trong nhóm: " + soNguoi);
	}

	private boolean kiemTraVaoNhom() {
		return true;
	}
	
	@Override
	public void onResume(){
		super.onResume();
		if(NetworkController._peer!=null&&NetworkController._peer.getAccount()!=null){
			NetworkController._peer.getAccount().inGroup=false;
		}
	}
	
}
